package so.ifl.servcie.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import so.ifl.commons.tools.Constant;
import so.ifl.commons.tools.StringHelper;
import so.ifl.dao.PayRecordDao;
import so.ifl.dao.StudentInfoDao;
import so.ifl.dao.SysDao;
import so.ifl.dao.TranRecordDao;
import so.ifl.servcie.TransactionService;
import so.ifl.vo.PayRecordVo;
import so.ifl.vo.StudentVo;
import so.ifl.vo.TranRecordVo;
import so.ifl.vo.Union1Vo;

/**
 *create by aaron.lu
 *create at 2012-2-2 下午11:55:11
 *<mailto: atyuan.lu@gmail.com>
 **/
@Service("transactionService")
public class TransactionServiceImpl implements TransactionService {
	
	@Autowired
	private TranRecordDao tranRecordDao;
	
	@Autowired
	private PayRecordDao payRecordDao;
	
	@Autowired
	private StudentInfoDao studentInfoDao;
	
	@Autowired
	private SysDao sysDao;
	
	/**
	 * 查询缴费记录<关联查询>
	 * create by aaron.lu
	 * create at 2012-5-9 上午12:01:54
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	public List<TranRecordVo> queryTranRecordAllInfo(Map<String, Object> params){
		return tranRecordDao.queryTranRecordAllInfo(params);
	}
	
	/**
	 * 查询交易记录
	 * create by aaron.lu
	 * create at 2012-2-4 下午3:19:15
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @param offset
	 * @param limit
	 * @return
	 *
	 */
	public List<TranRecordVo> queryTranRecord(Map<String, Object> params, int offset, int limit){
		return tranRecordDao.queryTranRecord(params, new RowBounds(offset, limit));
	}
	
	/**
	 * 统计交易记录
	 * create by aaron.lu
	 * create at 2012-2-4 下午3:21:02
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	public int countTranRecord(Map<String, Object> params){
		return tranRecordDao.countTranRecord(params);
	}
	
	/**
	 * 插入交易记录
	 * create by aaron.lu
	 * create at 2012-2-4 下午3:22:03
	 * <mailto: atyuan.lu@gmail.com>
	 * @param tranRecordVo
	 *
	 */
	public void insertTranRecord(TranRecordVo tranRecordVo){
		tranRecordDao.insertTranRecord(tranRecordVo);
	}
	
	/**
	 * 交易保存
	 * create by aaron.lu
	 * create at 2012-2-5 上午2:08:15
	 * <mailto: atyuan.lu@gmail.com>
	 * @param tranRecordVo
	 * @throws Exception
	 *
	 */
	public void tran(TranRecordVo tranRecordVo) throws Exception{
		Integer record_id_ = tranRecordVo.getRecordId();
		
		Map<String, Object> pay_params = new HashMap<String, Object>();
		pay_params.put("record_id_", record_id_);
		pay_params.put("student_id_", tranRecordVo.getStudentId());
		
		List<PayRecordVo> pay_list = payRecordDao.queryPayRecordOfStudent(pay_params, new RowBounds(0, 100));
		
		if (null != pay_list && pay_list.size() > 0) {
			PayRecordVo payRecordVo = pay_list.get(0);
			
			//本次累积付款金额
			Float pay_now = (null != payRecordVo.getPayEven()?payRecordVo.getPayEven():0) + tranRecordVo.getMoney();
			
			if (pay_now.floatValue() > payRecordVo.getShouldPay().floatValue()) {
				//加入本次付款>未付款项则报错。
				throw new Exception("本次付款大于未付款项..");
			}else{
				
				//保存消费记录
				tranRecordDao.insertTranRecord(tranRecordVo);
				
				if (null != payRecordVo.getStudentId()) {
					//更新累积付款金额
					payRecordVo.setPayEven(pay_now);	
					payRecordDao.updatePayRecord(payRecordVo);
				}
				
			}
			
			if (StringHelper.isNotEmpty(tranRecordVo.getTranNode()) && tranRecordVo.getTranNode().equals(Constant.NEW_TRAN_RECORD) 
					&& (tranRecordVo.getRecordId() == 7 || tranRecordVo.getRecordId() == 6 || tranRecordVo.getRecordId() == 3)) {//新增消费记录时,但科目一、桩试、路试需要补考的话需要缴费后预约
				List<StudentVo> students = studentInfoDao.queryStudentWithParamAndPagination(pay_params, new RowBounds(0, 1));
				if (null != students && students.size() > 0) {
					//
					StudentVo studentVo = students.get(0);
					studentVo.setNeedToPay(Constant.INT_FALSE_VALUE.toString());
					studentInfoDao.updateStudent(studentVo);
				}
			}
		}
		
	}
	
	/**
	 * 查询交易信息
	 * create by aaron.lu
	 * create at 2012-3-25 下午10:43:18
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	public List<TranRecordVo> queryTranInfo(Map<String, Object> params, int offset, int limit){
		return tranRecordDao.queryTranRecordAllInfo(params, new RowBounds(offset, limit));
	}
	
	/**
	 * 统计
	 * create by aaron.lu
	 * create at 2012-3-25 下午10:44:01
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	public int countTranInfo(Map<String, Object> params){
		return tranRecordDao.countTranRecordAllInfo(params);
	}
	
	/**
	 * 求和交易信息
	 * create by aaron.lu
	 * create at 2012-3-25 下午10:48:31
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	public float sumTranInfo(Map<String, Object> params){
		return tranRecordDao.sumTranInfo(params);
	}
	
	/**
	 * 更新交易记录
	 * create by aaron.lu
	 * create at 2012-5-20 上午12:39:33
	 * <mailto: atyuan.lu@gmail.com>
	 * @param tranRecordVo
	 *
	 */
	public void updateTranRecord(TranRecordVo tranRecordVo){
		tranRecordDao.updateTranRecord(tranRecordVo);
	}
	
}
